package com.chuangjiangx.member.stored.ddd.application;

import com.chuangjiangx.dddbase.application.Application;

import com.chuangjiangx.member.stored.ddd.application.command.CreateRechargeRuleCommand;
import com.chuangjiangx.member.stored.ddd.application.command.DeleteRechargeRuleCommand;
import com.chuangjiangx.member.stored.ddd.application.command.DisableRechargeRuleCommand;
import com.chuangjiangx.member.stored.ddd.application.command.EnableRechargeRuleCommand;
import com.chuangjiangx.member.stored.ddd.domain.service.MbrRechargeRuleService;
import com.chuangjiangx.domain.shared.model.Enable;
import com.chuangjiangx.member.basic.ddd.domain.model.MerchantId;
import com.chuangjiangx.member.stored.ddd.domain.model.IsDelete;
import com.chuangjiangx.member.stored.ddd.domain.model.MbrGiftType;
import com.chuangjiangx.member.stored.ddd.domain.model.CreateRechargeRule;
import com.chuangjiangx.member.stored.ddd.domain.model.DeleteRechargeRule;
import com.chuangjiangx.member.stored.ddd.domain.model.DisableRechargeRule;
import com.chuangjiangx.member.stored.ddd.domain.model.EnableRechargeRule;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.util.Date;


/**
 * 储值规则
 */
@Service
public class MbrRechargeRuleApplication implements Application {


    private final Logger logger = LoggerFactory.getLogger(MbrRechargeRuleApplication.class);

    @Autowired
    private MbrRechargeRuleService mbrRechargeRuleService;

    /**
     * 创建储值规则
     */
    @Transactional(isolation = Isolation.REPEATABLE_READ, rollbackFor = Exception.class)
    public void createRechargeRule(CreateRechargeRuleCommand command) throws Exception {
        logger.info("创建储值规则参数 {}：" + command);
        Assert.notNull(command.getMerchantId(), "商户id不能为空");
        CreateRechargeRule createRechargeRule = new CreateRechargeRule(command.getName(),
                command.getAmount(),
                command.getGiftScore(),
                command.getGiftAmount(),
                command.getCouponNumber(),
                command.getGiftCoupon(),
                Enable.getEnable(command.getEnable()),
                IsDelete.NOTDELETE,
                new Date(),
                new Date(),
                new MerchantId(command.getMerchantId()),
                MbrGiftType.getGiftType(command.getGiftType()));
        mbrRechargeRuleService.createRechargeRule(createRechargeRule);
    }

    /**
     * 启用储值规则
     */
    @Transactional(isolation = Isolation.REPEATABLE_READ, rollbackFor = Exception.class)
    public void enableRechargeRule(EnableRechargeRuleCommand command) throws Exception {
        logger.info("启用储值规则的参数,{}:" + command);
        Assert.notNull(command.getMerchantId(), "商户id不能为空");
        Assert.notNull(command.getStoredRulesId(), "储值规则id 不能为空");
        EnableRechargeRule enableRechargeRule = new EnableRechargeRule(command.getEnable(),
                command.getStoredRulesId(),command.getMerchantId());
        mbrRechargeRuleService.enableRechargeRule(enableRechargeRule);

    }

    /**
     * 禁用储值规则
     */
    @Transactional(isolation = Isolation.REPEATABLE_READ, rollbackFor = Exception.class)
    public void disableRechargeRule(DisableRechargeRuleCommand command) throws Exception {
        logger.info("不启用储值规则参数,{}:" + command);
        Assert.notNull(command.getMerchantId(), "商户id不能为空");
        Assert.notNull(command.getStoredRulesId(), "储值规则id 不能为空");
        DisableRechargeRule disableRechargeRule = new DisableRechargeRule(command.getEnable(),
                command.getStoredRulesId(),
                command.getMerchantId());
        mbrRechargeRuleService.disableRechargeRule(disableRechargeRule);
    }

    /**
     * 删除储值规则
     */
    @Transactional(isolation = Isolation.REPEATABLE_READ, rollbackFor = Exception.class)
    public void delRechargeRule(DeleteRechargeRuleCommand command) throws Exception {
        logger.info("删除储值规则参数,{}:" + command);
        Assert.notNull(command.getMerchantId(), "商户id不能为空");
        Assert.notNull(command.getStoredRulesId(), "储值规则id 不能为空");

        DeleteRechargeRule deleteRechargeRule = new DeleteRechargeRule(command.getEnable(),
                command.getStoredRulesId(),
                command.getMerchantId());
        mbrRechargeRuleService.delRechargeRule(deleteRechargeRule);
    }


    /**
     * 开启自定义储值开关
     *
     * @param merchantId 商户id
     */
    public void openRechargeRule(Long merchantId) {
        Assert.notNull(merchantId, "商户id不能为空");
        mbrRechargeRuleService.openRechargeRule(merchantId);
    }


    /**
     * 关闭自定储值开关
     *
     * @param merchantId 商户id
     */
    public void closeRechargeRule(Long merchantId) throws Exception {
        Assert.notNull(merchantId, "商户id不能为空");
        mbrRechargeRuleService.closeRechargeRule(merchantId);
    }

}
